package com.example.myapplication.old;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.old.adapter.ChatAdapter;
import com.example.myapplication.entity.Msg;
import com.example.myapplication.old.entity.User1;

import java.util.ArrayList;
import java.util.List;

public class ChatActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private EditText msg;
    private TextView btn;
    private Context context;
    private List<Msg>msgList=new ArrayList<>();
    private long exitTime = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        context=this;
        recyclerView=findViewById(R.id.recyclerlist);
        msg=findViewById(R.id.tv_msg);
        btn=findViewById(R.id.tv_send);
        btn.setEnabled(false);
        TextView tittle =findViewById(R.id.tittle);
        TextView back_view = findViewById(R.id.back_view);
        User1 user1 = getIntent().getParcelableExtra("Contact");
        tittle.setText(user1.getUsername());
        back_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        RecyclerView.LayoutManager manager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(manager);
        ChatAdapter adapter = new ChatAdapter();
        recyclerView.setAdapter(adapter);
        msg.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(!TextUtils.isEmpty(msg.getText().toString())){
                    btn.setEnabled(true);
                }
                else{
                    btn.setEnabled(false);
                }
            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String edtmsg = msg.getText().toString();
                Msg msg1 = new Msg();
                msg1.setMsg(edtmsg);
                adapter.setmsg(msg1);
                msg.setText("");
                manager.scrollToPosition(adapter.getItemCount()-1);
            }
        });
        //initData();
        adapter.setimg(msgList);

    }

    @Override
    public void onBackPressed() {
        if(System.currentTimeMillis() - exitTime>2000){
            Toast.makeText(getApplicationContext(), "再按一次退出", Toast.LENGTH_SHORT).show();
            exitTime=System.currentTimeMillis();
        }
        else{
            super.onBackPressed();
        }
    }

    public void initData(){
        for(int i=0;i<20;i++){
            Msg msg = new Msg();
            msg.setType(i % 2 + 3);
            msg.setImg(R.drawable.a1);
            msgList.add(msg);
        }
    }

}